package terminal;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * .
 *
 * @author sunhao(sunhao.java@gmail.com)
 * @version 1.0 2016/12/12 下午2:49
 */
public class Terminal {
    public static void main(String[] args) {
        callShell("ls -al");

        try {
            Process process = Runtime.getRuntime().exec("/sbin/ifconfig");

            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            List<String> processList = new ArrayList<>();
            while ((line = input.readLine()) != null) {
                processList.add(line);
            }
            input.close();

            for (String l : processList) {
                System.out.println(l);
            }

            BufferedReader inputError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String lineError;
            List<String> processListError = new ArrayList<>();
            while ((lineError = inputError.readLine()) != null) {
                processListError.add(lineError);
            }
            inputError.close();

            for (String l : processListError) {
                System.out.println(l);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void callShell(String shellString) {
        try {
            Process process = Runtime.getRuntime().exec(shellString);
            int exitValue = process.waitFor();
            if (0 != exitValue) {
                System.err.println("call shell failed. error code is :" + exitValue);
            }
        } catch (Throwable e) {
            System.err.println("call shell failed. " + e);
        }

        System.out.println("call shell successed!");
    }
}
